//
//  StatuPictureView.m
//  OC-微博
//
//  Created by Apple on 15/11/13.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "StatuPictureView.h"
#import "StatusPictureViewCell.h"
#import "StatusViewModel.h"
/// 可重用表示符号
NSString *StatusPictureCellId = @"StatusPictureCellId";
@interface StatuPictureView()
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation StatuPictureView
#pragma mark /*******************  构造函数  *******************/
- (instancetype)init
{
    UICollectionViewFlowLayout*layout=[[UICollectionViewFlowLayout alloc]init];
    
    self.layout = layout;
    //设置间隙
    int StatusPictureViewItemMargin = 8;
    self.layout.minimumLineSpacing = StatusPictureViewItemMargin;
    self.layout.minimumLineSpacing = StatusPictureViewItemMargin;
    
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        [self registerClass:[StatusPictureViewCell  class] forCellWithReuseIdentifier:StatusPictureCellId];
        self.backgroundColor = [[UIColor alloc]initWithWhite:0.8 alpha:1.0];
        self.dataSource = self;
    }
    return self;
}
#pragma mark /*******************  数据源方法  *******************/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.viewM.thumbnailsURL.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    StatusPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:StatusPictureCellId forIndexPath:indexPath];
    cell.imageURL = self.viewM.thumbnailsURL[indexPath.item];
    return cell;
}
#pragma mark /*******************  懒加载  *******************/
- (void)setViewM:(StatusViewModel *)viewM {
    _viewM = viewM;
    [self sizeToFit];
    [self reloadData];
    
}
- (CGSize)sizeThatFits:(CGSize)size {
    
    return [self calcViewSize];
}
#pragma mark /*******************  计算视图大小  *******************/
- (CGSize)calcViewSize {
    // 照片之间的间距
    int StatusPictureViewItemMargin = 8;
    //1.准备
    //每行的照片数量
    int rowCount = 3;
    //最大宽度
    double maxWidth = [UIScreen mainScreen].bounds.size.width - 2 * StatusCellMargin;
    double itemWidth = (maxWidth - 2 * StatusPictureViewItemMargin) / rowCount - 0.01;
    self.layout.itemSize = CGSizeMake(itemWidth, itemWidth);
    //3.获取图片数量
    NSUInteger count = self.viewM.thumbnailsURL.count;
    //4.计算开始
    //1>没有图片
    if (count == 0) {
        return CGSizeZero;
    }
    //2>一张图片
    if (count == 1) {
        CGSize size = CGSizeMake(150, 120);
        //内部图片的大小
        self.layout.itemSize = size;
        //配图图片的大小
        return size;
        
    }
    //3>四张图片
    if (count == 4) {
        CGFloat w = 2 * itemWidth + StatusPictureViewItemMargin;
        return CGSizeMake(w+8, w+8);
    }
    //4>其他照片
    CGFloat row = ((count - 1) / rowCount + 1);
    CGFloat  h = row * itemWidth + (row - 1) * StatusPictureViewItemMargin;
    CGFloat  w = rowCount * itemWidth + (rowCount - 1) * StatusPictureViewItemMargin;
    
    return CGSizeMake(w+8, h+8);
}
@end
